package server;

import java.sql.SQLException;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

import dataHandlers.DataControlerInt;



public class Notifier implements Runnable {

	public static int FRIEND_ADDED_POST = 0;
	public static int CREATORS_TOPIC_UPDATED = 1;
	public static int NEW_POST_IN_CURRENT_TOPIC = 2;
	public static int FRIEND_UPDATED_POST = 3;
	public static int POST_UPDATED_IN_CURRENT_TOPIC = 4;
	public static int POST_DELETED_IN_CURRENT_TOPIC =7;
	public static int FRIEND_DELETED_POST = 8;
	
	
	
	
	public static int POST_UPDATED = 5;
	public static int POST_DELETED = 6;
	
	
	
	private LinkedBlockingQueue<NotificationMsg> _inBuffer;
	private ConcurrentHashMap<String, LogedOnUser> _loggedInUsers;
	private DataControlerInt _dc;
	
	
	public Notifier(ConcurrentHashMap<String, LogedOnUser> loggedUsers,DataControlerInt dc)
	{
		this._loggedInUsers = loggedUsers;
		this._inBuffer = new LinkedBlockingQueue<NotificationMsg>();
		this._dc = dc;
	}
	
	
	public void addMsg(NotificationMsg msg)
	{
		
		try {
			this._inBuffer.put(msg);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		
		while (true)
		{
			Vector<NotificationMsg> msgs = new Vector<NotificationMsg>();
			try {
				msgs.add(this._inBuffer.take());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return;
			}
			
			NotificationMsg ms;
			
				while(( ms = this._inBuffer.poll())!= null)
					msgs.add(ms);
			
			for(NotificationMsg msg:msgs)
			{
				try{
				System.out.println("type enterance:"+msg.getType());
				String userName = msg.getUserName();
				
				String topicCreator = this._dc.getTopicCreator(msg.getForum().getId(), msg.getTopic().getId());
				
				//add message to the creator of the topic
				
				
				if ((this._loggedInUsers.containsKey(topicCreator))&&(!userName.equals(topicCreator)))
				{
					NotificationMsg creatorMsg = msg.clone();
					creatorMsg.setType(Notifier.CREATORS_TOPIC_UPDATED);
					this._loggedInUsers.get(topicCreator).addMsg(creatorMsg);
				}
				
				//add message to all logged on users who are looking in the topic of the new post
				for (LogedOnUser lUser:this._loggedInUsers.values())
				{
					if ((lUser.getCurr_topic() == msg.getTopic().getId())&&(lUser.getCurr_forum() == msg.getForum().getId()))
					{
						NotificationMsg TopicMsg = msg.clone();
							if (msg.getType()==Notifier.POST_UPDATED)
								TopicMsg.setType(Notifier.POST_UPDATED_IN_CURRENT_TOPIC);
							else
								if (msg.getType()==Notifier.POST_DELETED)
									TopicMsg.setType(Notifier.POST_DELETED_IN_CURRENT_TOPIC);
								else
									TopicMsg.setType(Notifier.NEW_POST_IN_CURRENT_TOPIC);
						
						lUser.addMsg(TopicMsg);
						
					}
						
				}
				
				//add message to all friend who are intrested in message creator 
				
				for(String friend:this._loggedInUsers.get(userName).getInterestedInMe())
					if (this._loggedInUsers.containsKey(friend))
					{
						//System.out.println("inside");
						NotificationMsg friendMsg = msg.clone();
						if (msg.getType()==Notifier.POST_UPDATED)
							friendMsg.setType(Notifier.FRIEND_UPDATED_POST);
						else
							if (msg.getType()==Notifier.POST_DELETED)
								friendMsg.setType(Notifier.FRIEND_DELETED_POST);
						else
							friendMsg.setType(Notifier.FRIEND_ADDED_POST);
						this._loggedInUsers.get(friend).addMsg(friendMsg);
					}
				
				}catch(SQLException e){
					return;
				}
			
			}
		}

	}

}
